package com.lefevre.cms.common;

import lombok.Data;

import java.util.List;

/**
 * 分页数据封装
 *
 * @author Adolph
 */
@Data
public class CommonPage<T> {

    private Integer current;
    private Integer size;
    private Integer pages;
    private Long total;
    private List<?> list;

    /**
     * 将 Mybatis-plus分页后的list转为分页信息
     */
    public static <T> CommonPage<T> restPage(com.baomidou.mybatisplus.extension.plugins.pagination.Page<T> page) {
        CommonPage<T> result = new CommonPage<T>();
        result.setCurrent(Math.toIntExact(page.getCurrent()));
        result.setSize(Math.toIntExact(page.getSize()));
        result.setTotal(page.getTotal());
        result.setPages(Math.toIntExact(page.getPages()));
        result.setList(page.getRecords());
        return result;
    }

    /**
     * 将 SpringData-Elasticsearch分页后的list转为分页信息
     */
    public static <T> CommonPage<T> restPage(org.springframework.data.domain.Page<T> page) {
        CommonPage<T> result = new CommonPage<T>();
        result.setCurrent(page.getNumber());
        result.setSize(page.getSize());
        result.setTotal(page.getTotalElements());
        result.setPages(page.getTotalPages());
        result.setList(page.getContent());
        return result;
    }
}
